﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Web;

namespace RBAC.Web.Utilities
{
public class ImageClass
    {
        public String[] ConvertFileName(HttpPostedFileBase[] files) 
         {
            String[] fileName = new String[files.Length];

             for (int i = 0; i < files.Length; i++)
             {
                 fileName[i] = files[i].FileName.Substring(0, files[i].FileName.LastIndexOf(".")) + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") +  files[i].FileName.Substring(files[i].FileName.LastIndexOf("."), files[i].FileName.Length - files[i].FileName.LastIndexOf("."));
             }

             return fileName;
         }

        public String GetTopicFolder(String topic)
        {

            if (!Directory.Exists(topic))
                Directory.CreateDirectory(topic);

            return topic;
        }

        public String ConcateFileName(String[] fileNames)
        {
            String result = "";
            for (int i = 0; i < fileNames.Length; i++)
            {
                if (i > 0) result += ",";

                result += fileNames[i];
            }

            return result;
        }

        public String DeleteImage(String imagePath)
        {
            // ลบรูปใน physical address
            return "";
        }

        public String UpdateDataValue(String address, HttpPostedFileBase[] updateimages, String stateDelete, string className)
        {
            string result = "";
            List<String> listAddr = new List<String>();

            foreach (String splitAdd in address.Split(','))
            {
                listAddr.Add(splitAdd);
            }
            
            if(stateDelete != ""){

                foreach (String splitDel in stateDelete.Split(','))
                {
                    String splitDelAfterConvert = "";

                    if (splitDel.StartsWith(className + '\\'))
                    {
                        splitDelAfterConvert = splitDel.Replace((className + '\\'),String.Empty);
                    }

                    listAddr.Remove(splitDelAfterConvert);
                }
            
            }
            if (updateimages[0] != null)
            {
                String[] updImages = ConvertFileName(updateimages);
                foreach (String updImage in updImages)
                {
                    listAddr.Add(updImage); 
                   
                }

            }

            for (int i = 0; i < listAddr.Count; i++)
            {
                if (listAddr[i] != "")
                {
                    result += listAddr[i];

                    if(i < listAddr.Count-1)
                    {
                        result += ",";
                    }
                }
            }
                return result;
        }
   }

}